import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import createpinia from '@/pinia'
import { userStore } from '@/pinia/userStore'

const userstore = userStore(createpinia)

//路由
const routes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: () => import("@/views/Login/index.vue")
  },
  {
    path: "/reigster",
    component: () => import("@/views/register/index.vue")
  },
  {
    path: "/",
    redirect: "/dashboard",
    component: () => import("@/views/Home/index.vue"),
    children: [
      {
        path: "test:d?",
        component: () => import("@/views/Test.vue")
      },
      {
        path: "dashboard",
        component: () => import("@/views/Dashboard/index.vue")
      },
      {
        path: "project",
        component: () => import("@/views/project/index.vue")
      },
      {
        path: "calendar",
        component: () => import("@/views/Calendar/index.vue")
      },
      {
        path: "vacation",
        component: () => import("@/views/vacation/index.vue")
      },
      {
        path: "chat",
        component: () => import("@/views/chat/index.vue")
      }
    ]
  }];


const router = createRouter({
  routes,
  history: createWebHashHistory(),
})


router.beforeEach((to, _from, next) => {

  // 如果是login 注册 页面 放行
  if (to.path === '/login' || to.path === '/reigster') {
    next()
  } else {
    // 判断是否登录
    if (!userstore.isLogin) {
      next("/login?toPage=" + to.fullPath)
    } else {
      next()
    }
  }
})


export default router;